package com.homeparking.web.controller.common;

import com.alibaba.fastjson2.JSONObject;
import com.homeparking.common.core.domain.AjaxResult;
import com.homeparking.common.core.domain.entity.SysUser;
import com.homeparking.common.core.domain.model.LoginUser;
import com.homeparking.common.utils.SecurityUtils;
import com.homeparking.framework.web.service.SysPermissionService;
import com.homeparking.framework.web.service.TokenService;
import com.homeparking.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.homeparking.common.utils.WeChatUtils;

import java.util.Map;
import java.util.Set;


// Controller层
@RestController
@RequestMapping("/wechat")
public class WeChatController {
    @Autowired
    private ISysUserService userService;
    @Autowired
    private WeChatUtils weChatUtils;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;


    @GetMapping ("/getPhoneNumber")
    public AjaxResult getPhoneNumber(@RequestParam(name="code") String code,@RequestParam(name="openid") String openid) {
        String phoneNumber = weChatUtils.getPhoneNumber(code);


        SysUser user=userService.selectUserByOpenid(openid);

        if(user==null) {
            user=new SysUser();
            user.setPhonenumber(phoneNumber);
            user.setOpenid(openid);
            user.setNickName(phoneNumber);
            user.setUserName(phoneNumber);
            user.setDeptId(201L);
            user.setPassword(SecurityUtils.encryptPassword("admin123"));
            user.setCreateBy("wxsmall");
            user.setStatus("1");
            user.setUserType("01");
            userService.insertUser(user);
            user=userService.selectUserByOpenid(openid);
            user.setPassword(null);

        }

        // 生成新Token并返回
        LoginUser loginUser =new LoginUser();

        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        loginUser.setPermissions(permissions);

        loginUser.setUserId(user.getUserId());
        loginUser.setDeptId(user.getDeptId());
        loginUser.setUser(user);

        Map<String, String> tokenUser = tokenService.createToken(loginUser);

        tokenUser.put("openId",user.getOpenid());
        tokenUser.put("userName",user.getUserName());
        tokenUser.put("phonenumber",user.getPhonenumber());
        tokenUser.put("nickName",user.getNickName());



        return AjaxResult.success(tokenUser);


    }



    @PostMapping("/getwxopenid")
    public AjaxResult getWxOpenid(@RequestParam String code) {

        JSONObject wxId = weChatUtils.getWxOpenid(code);

        // 生成新Token并返回
        LoginUser loginUser =new LoginUser();
        SysUser user=  userService.selectUserByOpenid(wxId.getString("openid"));
        if(user==null){
            return AjaxResult.success(wxId);
        }
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        loginUser.setPermissions(permissions);

        loginUser.setUserId(user.getUserId());
        loginUser.setDeptId(user.getDeptId());
        loginUser.setUser(user);

        Map<String, String> token = tokenService.createToken(loginUser);

        for (Map.Entry<String, String> entry : token.entrySet()) {

            wxId.put(entry.getKey(), entry.getValue());

        }

        return AjaxResult.success(wxId);


    }
}




